{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:100% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# encoding: utf-8\n",
    "import pandas as pd\n",
    "from pandas import *\n",
    "import numpy as np\n",
    "from pymongo import MongoClient\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "client = MongoClient('localhost', 27017)\n",
    "db = client.futures3\n",
    "jd= db.jd\n",
    "\n",
    "start='20200310'\n",
    "broker2=['宏源期货','方正中期','英大期货','美尔雅期货','格林大华']\n",
    "\n",
    "jd = DataFrame(list(jd.find({'trade_date': {'$gte': start}})))\n",
    "\n",
    "jd=jd.loc[jd['broker']!='期货公司会员']\n",
    "jd['净持仓']=jd.apply(lambda x: x['long_hld'] - x['short_hld'], axis=1)\n",
    "\n",
    "# jd=jd.sort_values('净持仓',inplace=False)\n",
    "# jd=jd[jd['symbol']=='JD2005']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trade_date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>五少净空</th>\n",
       "      <th>净空汇总</th>\n",
       "      <th>五少占比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20200310</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-1535.0</td>\n",
       "      <td>-8016.0</td>\n",
       "      <td>0.191492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20200311</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4101.0</td>\n",
       "      <td>-12013.0</td>\n",
       "      <td>0.341380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20200312</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-5229.0</td>\n",
       "      <td>-14552.0</td>\n",
       "      <td>0.359332</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>20200313</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-5616.0</td>\n",
       "      <td>-15565.0</td>\n",
       "      <td>0.360810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>20200316</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3313.0</td>\n",
       "      <td>-12690.0</td>\n",
       "      <td>0.261072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>20200317</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3239.0</td>\n",
       "      <td>-13973.0</td>\n",
       "      <td>0.231804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>20200318</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4248.0</td>\n",
       "      <td>-17244.0</td>\n",
       "      <td>0.246347</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>20200319</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4480.0</td>\n",
       "      <td>-17055.0</td>\n",
       "      <td>0.262680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>20200320</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3746.0</td>\n",
       "      <td>-22897.0</td>\n",
       "      <td>0.163602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20200323</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4342.0</td>\n",
       "      <td>-21934.0</td>\n",
       "      <td>0.197958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>20200324</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4850.0</td>\n",
       "      <td>-23251.0</td>\n",
       "      <td>0.208593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>20200325</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-7646.0</td>\n",
       "      <td>-30447.0</td>\n",
       "      <td>0.251125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>20200326</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-9064.0</td>\n",
       "      <td>-29389.0</td>\n",
       "      <td>0.308415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>20200327</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-9857.0</td>\n",
       "      <td>-37372.0</td>\n",
       "      <td>0.263754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>20200330</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-12696.0</td>\n",
       "      <td>-44436.0</td>\n",
       "      <td>0.285714</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   trade_date  symbol     五少净空     净空汇总      五少占比\n",
       "1    20200310  JD2006  -1535.0  -8016.0  0.191492\n",
       "3    20200311  JD2006  -4101.0 -12013.0  0.341380\n",
       "5    20200312  JD2006  -5229.0 -14552.0  0.359332\n",
       "7    20200313  JD2006  -5616.0 -15565.0  0.360810\n",
       "9    20200316  JD2006  -3313.0 -12690.0  0.261072\n",
       "11   20200317  JD2006  -3239.0 -13973.0  0.231804\n",
       "13   20200318  JD2006  -4248.0 -17244.0  0.246347\n",
       "15   20200319  JD2006  -4480.0 -17055.0  0.262680\n",
       "17   20200320  JD2006  -3746.0 -22897.0  0.163602\n",
       "19   20200323  JD2006  -4342.0 -21934.0  0.197958\n",
       "21   20200324  JD2006  -4850.0 -23251.0  0.208593\n",
       "23   20200325  JD2006  -7646.0 -30447.0  0.251125\n",
       "25   20200326  JD2006  -9064.0 -29389.0  0.308415\n",
       "27   20200327  JD2006  -9857.0 -37372.0  0.263754\n",
       "29   20200330  JD2006 -12696.0 -44436.0  0.285714"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "jd=jd[jd['净持仓']<0]\n",
    "sums =jd.groupby(['trade_date', 'symbol'])['净持仓'].sum().reset_index(name='净空汇总')\n",
    "\n",
    "df=pd.DataFrame()\n",
    "for i in broker2:\n",
    "    try:\n",
    "        brokers = jd[jd['broker'] == i]\n",
    "        df2=pd.DataFrame(brokers)\n",
    "        df = df.append(df2)\n",
    "    except:\n",
    "        pass\n",
    "sums2 = df.groupby(['trade_date', 'symbol'])['净持仓'].sum().reset_index(name='五少净空')\n",
    "merge = pd.merge(sums2, sums, on=['trade_date', 'symbol'], how='outer').fillna(0)\n",
    "merge['五少占比']=merge.apply(lambda x: x['五少净空']/x['净空汇总'], axis=1)\n",
    "merge=merge[merge['symbol']=='JD2006']\n",
    "merge\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:100% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"640\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%matplotlib notebook\n",
    "#二行即可搞定画图中文乱码\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示负号\n",
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))\n",
    "# 画图\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示负号\n",
    "\n",
    "a= pd.DataFrame({'五少占比':np.array(merge['五少占比']),'五少净空':np.array(merge['五少净空'])},index=merge['trade_date'])\n",
    "ax = a.plot(\n",
    "    secondary_y=['五少净空'],\n",
    "    x_compat=True,\n",
    "    grid=True)\n",
    "\n",
    "ax.set_title(merge['symbol'].iloc[0]+\"五少占比-净空\")\n",
    "ax.set_ylabel('占比')\n",
    "ax.grid(linestyle=\"--\", alpha=0.3)\n",
    "\n",
    "ax.right_ax.set_ylabel('净空')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# jd=jd[jd['trade_date']=='20200327']\n",
    "\n",
    "\n",
    "# sort=jd.sort_values('净持仓',inplace=False)\n",
    "# plt.bar(range(len(sort['净持仓'])),sort['净持仓'])\n",
    "# plt.xticks(range(len(sort['broker'])),sort['broker'])\n",
    "# # plt.xlabel('品种')\n",
    "# plt.ylabel('净持仓价值比例')\n",
    "# plt.title(' 净持仓 '+sort['trade_date'].iloc[0])\n",
    "# plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签\n",
    "# plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trade_date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>4少净空</th>\n",
       "      <th>净空汇总</th>\n",
       "      <th>4少占比</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20200310</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-1535.0</td>\n",
       "      <td>-8016.0</td>\n",
       "      <td>0.191492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20200311</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3245.0</td>\n",
       "      <td>-12013.0</td>\n",
       "      <td>0.270124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20200312</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4392.0</td>\n",
       "      <td>-14552.0</td>\n",
       "      <td>0.301814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>20200313</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4728.0</td>\n",
       "      <td>-15565.0</td>\n",
       "      <td>0.303758</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>20200316</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-2557.0</td>\n",
       "      <td>-12690.0</td>\n",
       "      <td>0.201497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>20200317</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-2541.0</td>\n",
       "      <td>-13973.0</td>\n",
       "      <td>0.181851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>20200318</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3453.0</td>\n",
       "      <td>-17244.0</td>\n",
       "      <td>0.200244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>20200319</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3798.0</td>\n",
       "      <td>-17055.0</td>\n",
       "      <td>0.222691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>20200320</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-3746.0</td>\n",
       "      <td>-22897.0</td>\n",
       "      <td>0.163602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20200323</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4342.0</td>\n",
       "      <td>-21934.0</td>\n",
       "      <td>0.197958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>20200324</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-4850.0</td>\n",
       "      <td>-23251.0</td>\n",
       "      <td>0.208593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>20200325</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-6448.0</td>\n",
       "      <td>-30447.0</td>\n",
       "      <td>0.211778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>20200326</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-7278.0</td>\n",
       "      <td>-29389.0</td>\n",
       "      <td>0.247644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>20200327</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-7853.0</td>\n",
       "      <td>-37372.0</td>\n",
       "      <td>0.210131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>20200330</td>\n",
       "      <td>JD2006</td>\n",
       "      <td>-10689.0</td>\n",
       "      <td>-44436.0</td>\n",
       "      <td>0.240548</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   trade_date  symbol     4少净空     净空汇总      4少占比\n",
       "1    20200310  JD2006  -1535.0  -8016.0  0.191492\n",
       "3    20200311  JD2006  -3245.0 -12013.0  0.270124\n",
       "5    20200312  JD2006  -4392.0 -14552.0  0.301814\n",
       "7    20200313  JD2006  -4728.0 -15565.0  0.303758\n",
       "9    20200316  JD2006  -2557.0 -12690.0  0.201497\n",
       "11   20200317  JD2006  -2541.0 -13973.0  0.181851\n",
       "13   20200318  JD2006  -3453.0 -17244.0  0.200244\n",
       "15   20200319  JD2006  -3798.0 -17055.0  0.222691\n",
       "17   20200320  JD2006  -3746.0 -22897.0  0.163602\n",
       "19   20200323  JD2006  -4342.0 -21934.0  0.197958\n",
       "21   20200324  JD2006  -4850.0 -23251.0  0.208593\n",
       "23   20200325  JD2006  -6448.0 -30447.0  0.211778\n",
       "25   20200326  JD2006  -7278.0 -29389.0  0.247644\n",
       "27   20200327  JD2006  -7853.0 -37372.0  0.210131\n",
       "29   20200330  JD2006 -10689.0 -44436.0  0.240548"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding: utf-8\n",
    "import pandas as pd\n",
    "from pandas import *\n",
    "import numpy as np\n",
    "from pymongo import MongoClient\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "pd.set_option('display.width', None)  # 设置字符显示宽度\n",
    "pd.set_option('display.max_rows', None)  # 设置显示最大行\n",
    "pd.set_option('display.max_columns', None)  # 设置显示最大行\n",
    "\n",
    "client = MongoClient('localhost', 27017)\n",
    "db = client.futures3\n",
    "jd= db.jd\n",
    "\n",
    "start='20200310'\n",
    "broker2=['宏源期货','方正中期','英大期货','格林大华']\n",
    "\n",
    "jd = DataFrame(list(jd.find({'trade_date': {'$gte': start}})))\n",
    "\n",
    "jd=jd.loc[jd['broker']!='期货公司会员']\n",
    "jd['净持仓']=jd.apply(lambda x: x['long_hld'] - x['short_hld'], axis=1)\n",
    "\n",
    "# jd=jd.sort_values('净持仓',inplace=False)\n",
    "# jd=jd[jd['symbol']=='JD2005']\n",
    "\n",
    "jd=jd[jd['净持仓']<0]\n",
    "sums =jd.groupby(['trade_date', 'symbol'])['净持仓'].sum().reset_index(name='净空汇总')\n",
    "\n",
    "df=pd.DataFrame()\n",
    "for i in broker2:\n",
    "    try:\n",
    "        brokers = jd[jd['broker'] == i]\n",
    "        df2=pd.DataFrame(brokers)\n",
    "        df = df.append(df2)\n",
    "    except:\n",
    "        pass\n",
    "sums2 = df.groupby(['trade_date', 'symbol'])['净持仓'].sum().reset_index(name='4少净空')\n",
    "merge = pd.merge(sums2, sums, on=['trade_date', 'symbol'], how='outer').fillna(0)\n",
    "merge['4少占比']=merge.apply(lambda x: x['4少净空']/x['净空汇总'], axis=1)\n",
    "merge=merge[merge['symbol']=='JD2006']\n",
    "merge\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
